package com.liujie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liujie.common.BaseContext;
import com.liujie.common.R;
import com.liujie.entity.ShoppingCart;
import com.liujie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置当前购物车的用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //获取菜品id，用于判断添加进来的为菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        //通过LambdaQueryWrapper判断查询
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId())
                .eq(dishId!=null,ShoppingCart::getDishId,dishId)
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(lqw);

        //判断当前菜是否在购物车中，如果存在（不为空）
        if (one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }

        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(lqw);
        if (shoppingCarts.size()>0){
            return R.success(shoppingCarts);
        }
        return R.error("购物车中无菜品");
    }

    @DeleteMapping("/clean")
    public R<String> delete(){
        boolean remove = shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId, BaseContext.getCurrentId()));
        if (remove){
            return R.success("购物车已清空");
        }
        return R.error("购物车清空失败");
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        boolean b=false;
        if (number>1){
            one.setNumber(number-1);
            b = shoppingCartService.updateById(one);
        }else {
            one.setNumber(0);
            b = shoppingCartService.updateById(one);
        }
        if (b){
            return R.success(one);
        }
        return R.error("减少菜品数量失败");
    }
}
